package com.oig.sys.base.app.spider;

import cn.hutool.core.date.DateUtil;
import com.oig.common.constants.BasicConstants;
import com.oig.common.constants.CommonEnum;
import com.oig.sys.base.constants.SysBaseEnum;
import com.oig.sys.base.dto.CurrRateDto;
import com.oig.sys.base.service.ITbCurrRateService;
import com.oig.sys.base.service.ITbCurrencyService;
import com.oig.sys.base.vo.CurrRateVo;
import com.oig.sys.base.vo.CurrencyVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
public class CurrRateBocPipeline implements Pipeline {

    private final ITbCurrRateService currRateService;
    private final ITbCurrencyService currencyService;

    public CurrRateBocPipeline(ITbCurrRateService currRateService, ITbCurrencyService currencyService){
        this.currRateService = currRateService;
        this.currencyService = currencyService;
    }


    @Override
    public void process(ResultItems resultItems, Task task) {
        log.debug("process pipeline :ResultItems-{}-", resultItems);
        List<String> currNameList = resultItems.get("currNameList");
        List<String> bankBuyRateList = resultItems.get("bankBuyRateList");
        List<String> currRateList = resultItems.get("currRateList");
        List<String> customsRateList = resultItems.get("customsRateList");
        List<String> currDateList = resultItems.get("currDateList");
        List<String> currTimeList = resultItems.get("currTimeList");
        Map<String, String> needCurrMap = this.getNeedCurrency();
        log.debug("process pipeline :needCurrMap-{}-", needCurrMap);
        List<CurrRateVo> currRateVos = new ArrayList<>();
        for (int i=0;i<currNameList.size();i++){
            if (StringUtils.isBlank(currNameList.get(i))||!needCurrMap.containsKey(currNameList.get(i).trim())){
                log.debug("continue name={}",currNameList.get(i).trim());
                continue;
            }
            log.debug("spider curr name={}",currNameList.get(i));
            String currName = currNameList.get(i);
            String bankBuyRate = bankBuyRateList.get(i);    //现汇买入价
            String currRate = currRateList.get(i);          //现汇卖出价
            String customsRate = customsRateList.get(i);    //折算价，海关汇率取这个
            String currDate = currDateList.get(i);
            String currTime = currTimeList.get(i);
            log.debug("curr:{}-{}-{}-{}", currName, currRate, currDate, currTime);
            if (StringUtils.isNotBlank(currRate) && StringUtils.isNotBlank(currRate)){
                CurrRateDto currRateDto = new CurrRateDto();
                currRateDto.setCurrency(needCurrMap.get(currName));
                BigDecimal rmbRate = new BigDecimal(currRate.trim());
                BigDecimal rmbGivenRate = new BigDecimal(customsRate.trim());
                BigDecimal buyRate = BigDecimal.ZERO;
                if (StringUtils.isNotBlank(bankBuyRate)) buyRate = new BigDecimal(bankBuyRate.trim());
                currRateDto.setRmbRate(rmbRate.divide(new BigDecimal(100), BasicConstants.RATE_SCALE, RoundingMode.HALF_EVEN));    //银行卖出汇率
                currRateDto.setBankRmbBrate(buyRate.divide(new BigDecimal(100), BasicConstants.RATE_SCALE,RoundingMode.HALF_EVEN)); //银行买入
                currRateDto.setBankRmbSrate(currRateDto.getRmbRate());  //银行卖出汇率
                currRateDto.setBankRmbGiven(rmbGivenRate.divide(new BigDecimal(100), BasicConstants.RATE_SCALE, RoundingMode.HALF_EVEN));  //折算汇率
                currRateDto.setRateTime(DateUtil.parseDateTime(currDate.trim()));
                currRateDto.setCustomsFlag(SysBaseEnum.CurrRateType.PUNCTUAL.getCode());
                currRateDto.setRateSource("中国银行(BOC)");
                log.debug("add curr rate {}", currRateDto);
                CurrRateVo currRateVo = currRateService.addTbCurrRate(currRateDto);       //没有授权问题
                currRateVos.add(currRateVo) ;
            }
        }

    }


    private Map<String, String> getNeedCurrency(){
        Map<String, String> needCurrMap = new HashMap<>();
        List<CurrencyVo> currencyVos = currencyService.listAll(CommonEnum.CommonStatusEnum.OK.getCode());
        for (CurrencyVo currencyVo : currencyVos){
            needCurrMap.put(currencyVo.getName(), currencyVo.getCode());
        }
        return needCurrMap;
    }


}
